/**
 * 
 */
package action;
import java.util.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import dao.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Administrator
 *
 */

public class SubmitAction extends ActionSupport{
	private Integer problemId;
	private String userId;
	private Short language=0;
	private String source;
	
	
	public void setProblemId(Integer problemId) {
		this.problemId = problemId;
	}
	public Integer getProblemId() {
		return problemId;
	}
	
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserId() {
		return userId;
	}
	
	public void setLanguage(Short language) {
		this.language = language;
	}
	public Short getLanguage() {
		return language;
	}
	
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	

	@Override
	public String execute() throws Exception{
		
		ActionContext ac=ActionContext.getContext();
		Map session=ac.getSession();
		userId=(String)session.get("User");
		System.out.println("submit.action:problemId="+problemId);
		System.out.println("submit.action:language="+language);
		
		if(language==0){
            System.out.println("第一次进入Submit.action");
            return INPUT;
        }
		else if(problemId!=null&&language!=0&&source!=null){
			 System.out.println("进入Submit.action");
			 ProblemDAO pdao=new ProblemDAO();
			 if(pdao.findById(problemId)==null){
				 System.out.println("你输入的ProblemId不存在....");
				 addActionError("你输入的ProblemId不存在....");
				 return INPUT;
			 }
			 if(language!=1&&language!=2&&language!=3&&language!=4){
				 System.out.println("对不起，你输入的language我们没有提供...");
				 addActionError("对不起，你输入的language我们没有提供...");
				 return INPUT;
			 }
			 int codelength=source.length();
			 java.sql.Timestamp submitDate = new java.sql.Timestamp(System.currentTimeMillis());
			 Solution s=new Solution(problemId,userId,submitDate,language,codelength);
			 SourceCode sc=new SourceCode();
			 sc.setSource(source);
			 sc.setSolution(s);
			 SolutionDAO dao=new SolutionDAO();
			 SourceCodeDAO sdao=new SourceCodeDAO();
			 Session hsession=HibernateSessionFactory.getSession();
			 hsession.beginTransaction();
			 dao.save(s);
			 sdao.save(sc);
		     hsession.getTransaction().commit();
			 return SUCCESS;
		}
		else{
		    //addActionError("你输入的信息不完整...,请重现输入！");
			return INPUT;
		}
			
	}




	
}